package info.itloser.learnkotlindemo.ui.simple

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import info.itloser.learnkotlindemo.R
import info.itloser.learnkotlindemo.adapter.FruitListAdapter
import info.itloser.learnkotlindemo.basic.Constants.icons
import info.itloser.learnkotlindemo.bean.Fruit
import info.itloser.learnkotlindemo.databinding.ActivityListViewBinding

class ListViewActivity : AppCompatActivity() {

    private lateinit var binding: ActivityListViewBinding

    /**
     * ArrayAdapter 用的数据
     */
    private val itemLayout = android.R.layout.simple_list_item_1
    private val data = listOf(
        "Apple", "Banana", "Orange", "Watermelon",
        "Pear", "Grape", "Pineapple", "Strawberry", "Cherry", "Mango",
        "Apple", "Banana", "Orange", "Watermelon", "Pear", "Grape",
        "Pineapple", "Strawberry", "Cherry", "Mango"
    )

    /**
     * FruitAdapter : ArrayAdapter 用的数据
     */
    private val fruits = listOf(
        Fruit(icons[0], "百香果"),
        Fruit(icons[1], "马蹄"),
        Fruit(icons[2], "菠萝"),
        Fruit(icons[3], "草莓"),
        Fruit(icons[4], "车厘子"),
        Fruit(icons[5], "大蒜"),
        Fruit(icons[0], "百香果"),
        Fruit(icons[1], "马蹄"),
        Fruit(icons[2], "菠萝"),
        Fruit(icons[3], "草莓"),
        Fruit(icons[4], "车厘子"),
        Fruit(icons[5], "大蒜"),
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityListViewBinding.inflate(layoutInflater)
        setContentView(binding.root)

        //默认的 ArrayAdapter
//        val listAdapter = ArrayAdapter(this, itemLayout, data)
//        binding.lvMain.adapter = listAdapter

        //自定义的 FruitAdapter : ArrayAdapter
        val fruitAdapter = FruitListAdapter(this, R.layout.item_fruit, fruits)
        //设置适配器
        binding.lvMain.adapter = fruitAdapter

        //点击事件
        binding.lvMain.setOnItemClickListener { _, _, position, _ ->
            Toast.makeText(applicationContext, "你点击了" + fruits[position].name, Toast.LENGTH_SHORT)
                .show()
        }

        //长按事件
        binding.lvMain.setOnItemLongClickListener { _, _, position, _ ->
            Toast.makeText(applicationContext, "你长按了" + fruits[position].name, Toast.LENGTH_SHORT)
                .show()
            false
        }

    }
}